package messages;

import java.sql.ResultSet;

import javax.swing.JOptionPane;

import controllers.DBConnection;
import ocsf.server.ConnectionToClient;
/**
 * subscribe a customer message 
 * handle with DB , getting info and INSERT specific customer 

 */
public class SubscribingByCustomerMessage {

	String[] userArr;
	ConnectionToClient client;
	

	public SubscribingByCustomerMessage(String[] strMsg, ConnectionToClient client){
	
		this.userArr=strMsg;
		this.client=client;	
	}
	
	
	public void activationSub(){
		
		String id=this.userArr[1];
		String subscType=this.userArr[2];
		int type = 17;
		double bill = 0;
		int subscriptionType;
		double memBill;
		int purch=0;
		
		//check Subscription type
		if(userArr[2].equals("Package")){
			type=2;
			bill=1400;
			purch=200;
		}
		else if(userArr[2].equals("Subscription information layer")){
			type=3;
			bill=50;
		}
		else if(userArr[2].equals("Member Location Time")){
			type=5;
			bill=300;
		}
		
		try{
			/**
			 * making a connection with DB
			 */
		DBConnection conn=new DBConnection();
		java.sql.PreparedStatement pr= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.customer " + "WHERE customerID ='" + userArr[1]+"';");
		ResultSet rs=pr.executeQuery();	
		if(!rs.next()){
			rs.close();
			pr.close();
			System.out.println(" customer not yet subscribed ");
			
			java.sql.PreparedStatement pr2 = conn.getDBConnection().prepareStatement("INSERT INTO sgi_system.customer (customerID, numPurchasesSample, numPurchasesPicture, SubscriptionType, SubscriptionStatus, bill, numOfPurchasesSubscribed) VALUES(?,?,?,?,?,?,?);");
			pr2.setString(1,id );
			pr2.setInt(2, 0);
			pr2.setInt(3,0);
			pr2.setInt(4,type);
			pr2.setInt(5, 1);			
			pr2.setDouble(6, bill);
			pr2.setInt(7, 200);
			pr2.executeUpdate();
			System.out.println(" update new subscriber succseed");
			JOptionPane.showMessageDialog(null,"  update new subscriber succseed ");
			client.sendToClient("updateNewCustomer");
			pr2.close();
			
		}
		
		else{//Exist member
						
			memBill=rs.getDouble("bill")+bill;
			subscriptionType=rs.getInt("SubscriptionType");
			int purchases=rs.getInt("numOfPurchasesSubscribed");
			int finalType;
			rs.close();
			
			if(subscriptionType % type==0  ){
				System.out.println(" member already subscribed to this type ");
				JOptionPane.showMessageDialog(null," member already subscribed to this type ");
			}
			
			// update new subscription  
			else {   
						java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "UPDATE `sgi_system`.`customer` SET `SubscriptionType`=?, `bill`=?, `numOfPurchasesSubscribed`=? WHERE `customerID`= ?");
			            pr1.setInt(1,type);
			            pr1.setDouble(2, memBill);
			            pr1.setInt(3,purch );
			            pr1.setString(4, id);
						pr1.executeUpdate();
			            System.out.println(" Update existing customer succseed");
			            JOptionPane.showMessageDialog(null," Update existing customer succseed ");
						client.sendToClient("UpdateExistingCustomer");
						pr1.close();						
				}
			
			
			}


		}catch (Exception e)
	       {
	    	   e.printStackTrace();
	    	   JOptionPane.showMessageDialog(null," Action Failed ");
	       
	       } 

		
		
		
		
	}
	
	
	
	
	
}
